package com.cyan.teamquestionnaire.service.Impl;

import com.cyan.teamquestionnaire.beans.PageInfo;
import com.cyan.teamquestionnaire.entity.Group;
import com.cyan.teamquestionnaire.entity.Questionnaire;
import com.cyan.teamquestionnaire.entity.User;
import com.cyan.teamquestionnaire.mappers.GroupMapper;
import com.cyan.teamquestionnaire.mappers.QuestionnaireMapper;
import com.cyan.teamquestionnaire.mappers.UserMapper;
import com.cyan.teamquestionnaire.service.GroupService;
import com.cyan.teamquestionnaire.service.QuestionnaireService;
import com.cyan.teamquestionnaire.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {

    @Resource
    GroupMapper groupMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    QuestionnaireMapper questionnaireMapper;

    //controller传过来创建者和租户，service生成gId等其他内容
    @Override
    public void createGroup(Group group) {
        //根据工具类获取一个唯一的ID值
        group.setGId(UUIDUtil.getOneUUID());
        groupMapper.create(group);
    }

    // 删除群组,同时在群组里面的用户信息也删除(group_user)
    @Override
    public boolean deleteGroup(String gId) {

        // 群组只有租户一个用户，就可以删除
        if (questionnaireMapper.getQuestionnaireByGid(gId).size() != 0) {
            return false;
        }
        groupMapper.delete(gId);
        groupMapper.deleteGroupUser(gId);
        return true;
    }

    // 无用户状态下可以更改, gName是新的组名
    @Override
    public boolean modifyGroup(Group group) {
        groupMapper.modify(group);
        return true;
    }

    // 加入用户到群组
    @Override
    public boolean addUserToGroup(String name, String gId) {
        // 检测，不能重复加入
        if (groupMapper.checkInGroup(name, gId) != null) return false;
        groupMapper.addUserToGroup(name, gId);
        return true;
    }

    // 从群组中删除用户
    @Override
    public boolean deleteUserFromGroup(String name, String gId) {
        // 不存在，删除失败（其实也算删除成功吧，呵呵）
        if (groupMapper.checkInGroup(name, gId) == null) return false;
        groupMapper.deleteUserFromGroup(name, gId);
        return true;
    }

    // 选出群组所有用户
    @Override
    public List<String> usersInGroup(String gId) {
        return groupMapper.usersInGroup(gId);
    }

    //选择用户创建的List ——  不分页
    @Override
    public List<Group> getGroupList(Group group) {
        String name = group.getUserName();
        String groupName = '%' + group.getGroupName() + '%';
        return groupMapper.getGroupList(name, groupName);
    }

    //选择用户创建的List ——  分页
    @Override
    public PageInfo<Group> getGroupListY(PageInfo<Group> pageInfo) {
        List<Group> list = groupMapper.getGroupListFuzzy(pageInfo.getName(), "%" + pageInfo.getQuery() + "%");
        pageInfo.setQId(userMapper.getUserForTenant(pageInfo.getName()));
        return (PageInfo<Group>) (PageInfo) splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list);
    }

    @Override
    //选择qid群组里面的用户列表————分页
    public PageInfo<User> usersInGroupFuzzyY(PageInfo<User> pageInfo) {
        List<String> list = groupMapper.usersInGroupFuzzy(pageInfo.getGId(), "%" + pageInfo.getQuery() + "%");
        List<User> list1 = new ArrayList<>();
        for (String name : list) {
            list1.add(userMapper.selectByName(name));
        }
        return (PageInfo<User>) (PageInfo) splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list1);
    }

    @Override
    //返回这个租户管理下的用户还没加入群组的用户
    //要想群组加入用户，首先要选择出来这个租户的所有用户，然后选择出来所有这个群组里面的用户，两者做差集
    public PageInfo<User> getRestUser(PageInfo<User> pageInfo) {
        List<String> list = groupMapper.getRestUser(pageInfo.getGId(), pageInfo.getName(), "%" + pageInfo.getQuery() + "%");
        List<User> list1 = userMapper.selectAllAnswerer("%" + pageInfo.getQuery() + "%");
        for (String name : list) {
            list1.add(userMapper.selectByName(name));
        }

        return (PageInfo<User>) (PageInfo) splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list1);
    }

    //分组用来分页
    // 传入相关页面信息，实现分页
    @Override
    public PageInfo<Object> splitPage(PageInfo<Object> pageInfo, List<Object> list) {
        // list传过来已经是所有结果，只是说，这个list可能是通过name,qid,title查询的
        pageInfo.setTotal(list.size());
        pageInfo.setList(new ArrayList<>());

        int pages = pageInfo.getPageNum();
        int pageSize = pageInfo.getPageSize();
        for (int i = (pages - 1) * pageSize, j = 0; i < list.size() && j < pageSize; i++, j++)
            pageInfo.getList().add(list.get(i));
        return pageInfo;
    }

    @Override
    public int countGroupInTenant(String name) {
        return groupMapper.countGroupInTenant(name);
    }

    //返回该租户下的所有群组list，分页模糊
    @Override
    public PageInfo<Group> GroupInTenantFuzzy(PageInfo<Group> pageInfo){
        List<Group> list = groupMapper.GroupInTenantFuzzy(pageInfo.getName(), "%" + pageInfo.getQuery() + "%");
        return (PageInfo<Group>) (PageInfo) splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list);
    }
}
